/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package residentialdatabasemanagement;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Logger;
import java.util.logging.Level;
import javax.swing.JOptionPane;

public class ConnectToSQL {
  Connection connection;
    public Connection getConnectToMSSQL()
    {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "khong tim thay Driver cho SQL");
        }
        try {
            connection = DriverManager.getConnection("jdbc:sqlserver://MUOI-PC\\SQLEXPRESS:1433;databaseName=QLChungCu","sa","123");
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "khong ket noi duoc database");
        }
       return connection;
    }
    public void Close()
    {
        if(connection!=null)
            try {
                if(connection.isClosed())
                    connection.close();
        } catch (SQLException ex) {
            Logger.getLogger(ConnectToSQL.class.getName()).log(Level.SEVERE, null, ex);
        }
        connection=null;
    }  
}
